package org.liaoxuefeng.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;

/**
 * @author: 默苍璃
 * @date: 2022-09-29 15:01
 * <p>
 * HikariCP 连接池 配置
 */
public class HikariCPConfig {

    private static HikariDataSource hikariDataSource;


    /**
     * 获取连接
     */
    public static DataSource getConnection() {

        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://localhost/jdbc?useSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8");
        config.setUsername("root");
        config.setPassword("root");
        // 连接超时：1秒
        config.addDataSourceProperty("connectionTimeout", "1000");
        // 空闲超时：60秒
        config.addDataSourceProperty("idleTimeout", "1000");
        // 最大连接数：10
        config.addDataSourceProperty("maximumPoolSize", "0");
        config.setMaximumPoolSize(5);

        if (hikariDataSource == null) {
            hikariDataSource = new HikariDataSource(config);
        }
        return hikariDataSource;
    }

}
